package com.fzhucc.modules.subject.service;

import com.fzhucc.base.PageDTO;
import com.fzhucc.modules.subject.domain.dto.*;
import com.fzhucc.modules.subject.domain.entity.SubjectHighfieldsType1;
import com.fzhucc.modules.subject.domain.entity.SubjectHighfieldsType2;
import com.fzhucc.modules.subject.domain.entity.SubjectHighfieldsType3;
import com.fzhucc.modules.subject.repository.SubjectHighfieldsType1Repository;
import com.fzhucc.modules.subject.repository.SubjectHighfieldsType2Repository;
import com.fzhucc.modules.subject.repository.SubjectHighfieldsType3Repository;
import com.fzhucc.modules.subject.service.mapstruct.SubjectHighfieldsType1Mapper;
import com.fzhucc.modules.system.domain.User;
import com.fzhucc.modules.system.repository.UserRepository;
import com.fzhucc.utils.FileUtil;
import com.fzhucc.utils.QueryHelp;
import com.fzhucc.utils.SecurityUtils;
import com.fzhucc.utils.ValidationUtil;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author yyg
 * @website https://fzhucc.com
 * @description 服务实现
 * @since 2022-09-26 00:00:00
 **/
@Service
@RequiredArgsConstructor
public class SubjectHighfieldsType1Service {

    @Value("${org.zd.fjs}")
    public String fjs;

    @Value("${org.zd.szd}")
    public String szd;

    @Value("${org.area.all2}")
    public String fjsstr;

    private final SubjectHighfieldsType1Repository subjectHighfieldsType1Repository;
    private final SubjectHighfieldsType1Mapper subjectHighfieldsType1Mapper;

    private final SubjectHighfieldsType2Repository subjectHighfieldsType2Repository;

    private final SubjectHighfieldsType3Repository subjectHighfieldsType3Repository;

    private final UserRepository userRepository;


    public PageDTO<SubjectHighfieldsType1QueryDto> queryAll(SubjectHighfieldsType1QueryCriteria criteria, Pageable pageable) {
        Page<SubjectHighfieldsType1> page = subjectHighfieldsType1Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder), pageable);
        PageDTO<SubjectHighfieldsType1QueryDto> pageDTO = PageDTO.by(page.map(subjectHighfieldsType1 -> subjectHighfieldsType1Mapper.toQueryDto(subjectHighfieldsType1)));
        return pageDTO;
    }

    public List<SubjectHighfieldsType1Result> querySubjectHighfieldsType() {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(fjs);
        List<SubjectHighfieldsType1Result> newlist = new ArrayList<SubjectHighfieldsType1Result>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1Result subjectHighfieldsType1Result = new SubjectHighfieldsType1Result();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1(dto.getHighfields1(), fjs);
            List<SubjectHighfieldsType2Result> list3 = new ArrayList<SubjectHighfieldsType2Result>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2Result subjectHighfieldsType2Result = new SubjectHighfieldsType2Result();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2Result.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2Result.setText(dto2.getName());
                        }
                        list3.add(subjectHighfieldsType2Result);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2Result subjectHighfieldsType2Result = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2Result.getValue(), fjs);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3Result> list5 = new ArrayList<SubjectHighfieldsType3Result>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3Result subjectHighfieldsType3Result = new SubjectHighfieldsType3Result();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setText(dto3.getName());
                                    }
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2Result.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1Result.setText(dto.getName());
            subjectHighfieldsType1Result.setValue(dto.getHighfields1());
            subjectHighfieldsType1Result.setChildren(list3);
            newlist.add(subjectHighfieldsType1Result);
        }
        return newlist;
    }

    public List<SubjectHighfieldsType1ResultH5> seeAll() {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(fjs);
        List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1ResultH5 subjectHighfieldsType1ResultH5 = new SubjectHighfieldsType1ResultH5();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1(dto.getHighfields1(), fjs);
            List<SubjectHighfieldsType2ResultH5> list3 = new ArrayList<SubjectHighfieldsType2ResultH5>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = new SubjectHighfieldsType2ResultH5();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2ResultH5.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2ResultH5.setText(dto2.getName());
                        }
                        subjectHighfieldsType2ResultH5.setChecked(false);
                        list3.add(subjectHighfieldsType2ResultH5);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2ResultH5.getValue(), fjs);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3ResultH5> list5 = new ArrayList<SubjectHighfieldsType3ResultH5>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3ResultH5 subjectHighfieldsType3Result = new SubjectHighfieldsType3ResultH5();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setText(dto3.getName());
                                    }
                                    subjectHighfieldsType3Result.setChecked(false);
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2ResultH5.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1ResultH5.setText(dto.getName());
            subjectHighfieldsType1ResultH5.setValue(dto.getHighfields1());
            subjectHighfieldsType1ResultH5.setChecked(false);
            subjectHighfieldsType1ResultH5.setChildren(list3);
            newlist.add(subjectHighfieldsType1ResultH5);
        }
        return newlist;
    }

    //查询  例:福建省;福州市;  类型
    public List<SubjectHighfieldsType1ResultH5> seeSecond(String area2) {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(fjs);
        List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1ResultH5 subjectHighfieldsType1ResultH5 = new SubjectHighfieldsType1ResultH5();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1With2(dto.getHighfields1(), fjs, area2);
            List<SubjectHighfieldsType2ResultH5> list3 = new ArrayList<SubjectHighfieldsType2ResultH5>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = new SubjectHighfieldsType2ResultH5();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2ResultH5.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2ResultH5.setText(dto2.getName());
                        }
                        subjectHighfieldsType2ResultH5.setChecked(false);
                        list3.add(subjectHighfieldsType2ResultH5);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2ResultH5.getValue(), fjs);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3ResultH5> list5 = new ArrayList<SubjectHighfieldsType3ResultH5>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3ResultH5 subjectHighfieldsType3Result = new SubjectHighfieldsType3ResultH5();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setText(dto3.getName());
                                    }
                                    subjectHighfieldsType3Result.setChecked(false);
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2ResultH5.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1ResultH5.setText(dto.getName());
            subjectHighfieldsType1ResultH5.setValue(dto.getHighfields1());
            subjectHighfieldsType1ResultH5.setChecked(false);
            subjectHighfieldsType1ResultH5.setChildren(list3);
            newlist.add(subjectHighfieldsType1ResultH5);
        }
        return newlist;
    }


    //查询  例:福建省;福州市;马尾区;  类型
    public List<SubjectHighfieldsType1ResultH5> seeThird(String area2, String area3) {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(fjs);
        List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1ResultH5 subjectHighfieldsType1ResultH5 = new SubjectHighfieldsType1ResultH5();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1With2(dto.getHighfields1(), fjs, area2);
            List<SubjectHighfieldsType2ResultH5> list3 = new ArrayList<SubjectHighfieldsType2ResultH5>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = new SubjectHighfieldsType2ResultH5();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2ResultH5.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2ResultH5.setText(dto2.getName());
                        }
                        subjectHighfieldsType2ResultH5.setChecked(false);
                        list3.add(subjectHighfieldsType2ResultH5);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2ResultH5 subjectHighfieldsType2ResultH5 = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2With3(subjectHighfieldsType2ResultH5.getValue(), fjs, area3);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3ResultH5> list5 = new ArrayList<SubjectHighfieldsType3ResultH5>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3ResultH5 subjectHighfieldsType3Result = new SubjectHighfieldsType3ResultH5();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setText(dto3.getName());
                                    }
                                    subjectHighfieldsType3Result.setChecked(false);
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2ResultH5.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1ResultH5.setText(dto.getName());
            subjectHighfieldsType1ResultH5.setValue(dto.getHighfields1());
            subjectHighfieldsType1ResultH5.setChecked(false);
            subjectHighfieldsType1ResultH5.setChildren(list3);
            newlist.add(subjectHighfieldsType1ResultH5);
        }
        return newlist;
    }


    public List<SubjectHighfieldsType1ResultH5> querySubjectHighfieldsTypeH5() {
        //TODO 根据登录用户是什么地区展示什么地区   admin返回所有
        try {
            Long userId = SecurityUtils.getCurrentUserId();
            Optional<User> res = userRepository.findById(userId);
            User user = res.get();
            Boolean isAdmin = user.getIsAdmin();
            if (isAdmin) {//admin用户看全部
                List<SubjectHighfieldsType1ResultH5> newlist = seeAll();
                return newlist;
            } else {
                String orgArea = user.getOrgArea();
                if (orgArea != null && !orgArea.equals("")) {
                    //TODO 福建省的看到所有
                    if (orgArea.equals(fjsstr)) {
                        List<SubjectHighfieldsType1ResultH5> newlist = seeAll();
                        return newlist;
                    } else {
                        String[] split = orgArea.split(";");
                        if (split.length == 2) {
                            List<SubjectHighfieldsType1ResultH5> newlist = seeSecond(split[1]);
                            return newlist;
                        } else if (split.length == 3) {
                            //如果是第三级只能查到自己
                            List<SubjectHighfieldsType1ResultH5> newlist = seeThird(split[1], split[2]);
                            return newlist;
                        } else {
                            List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
                            return newlist;
                        }
                    }
                } else {
                    List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
                    return newlist;
                }
            }
        } catch (Exception e) {
            List<SubjectHighfieldsType1ResultH5> newlist = new ArrayList<SubjectHighfieldsType1ResultH5>();
            return newlist;
        }
    }

    public List<SubjectHighfieldsType1Result> queryAreah5() {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(szd);
        List<SubjectHighfieldsType1Result> newlist = new ArrayList<SubjectHighfieldsType1Result>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1Result subjectHighfieldsType1Result = new SubjectHighfieldsType1Result();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1(dto.getHighfields1(), szd);
            List<SubjectHighfieldsType2Result> list3 = new ArrayList<SubjectHighfieldsType2Result>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2Result subjectHighfieldsType2Result = new SubjectHighfieldsType2Result();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2Result.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2Result.setText(dto2.getName());
                        }
                        list3.add(subjectHighfieldsType2Result);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2Result subjectHighfieldsType2Result = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2Result.getValue(), szd);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3Result> list5 = new ArrayList<SubjectHighfieldsType3Result>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3Result subjectHighfieldsType3Result = new SubjectHighfieldsType3Result();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setText(dto3.getName());
                                    }
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2Result.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1Result.setText(dto.getName());
            subjectHighfieldsType1Result.setValue(dto.getHighfields1());
            subjectHighfieldsType1Result.setChildren(list3);
            newlist.add(subjectHighfieldsType1Result);
        }
        return newlist;
    }

    public List<SubjectHighfieldsType1ResultLabel> queryProvinceLabel() {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(fjs);
        List<SubjectHighfieldsType1ResultLabel> newlist = new ArrayList<SubjectHighfieldsType1ResultLabel>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1ResultLabel subjectHighfieldsType1Result = new SubjectHighfieldsType1ResultLabel();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1(dto.getHighfields1(), fjs);
            List<SubjectHighfieldsType2ResultLabel> list3 = new ArrayList<SubjectHighfieldsType2ResultLabel>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2ResultLabel subjectHighfieldsType2Result = new SubjectHighfieldsType2ResultLabel();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2Result.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2Result.setLabel(dto2.getName());
                        }
                        list3.add(subjectHighfieldsType2Result);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2ResultLabel subjectHighfieldsType2Result = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2Result.getValue(), fjs);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3ResultLabel> list5 = new ArrayList<SubjectHighfieldsType3ResultLabel>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3ResultLabel subjectHighfieldsType3Result = new SubjectHighfieldsType3ResultLabel();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setLabel(dto3.getName());
                                    }
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2Result.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1Result.setLabel(dto.getName());
            subjectHighfieldsType1Result.setValue(dto.getHighfields1());
            subjectHighfieldsType1Result.setChildren(list3);
            newlist.add(subjectHighfieldsType1Result);
        }
        return newlist;
    }

    public List<SubjectHighfieldsType1ResultLabel> queryArea() {
        List<SubjectHighfieldsType1> list = subjectHighfieldsType1Repository.findByType(szd);
        List<SubjectHighfieldsType1ResultLabel> newlist = new ArrayList<SubjectHighfieldsType1ResultLabel>();
        for (int i = 0; i < list.size(); i++) {
            SubjectHighfieldsType1ResultLabel subjectHighfieldsType1Result = new SubjectHighfieldsType1ResultLabel();
            SubjectHighfieldsType1 dto = list.get(i);
            List<SubjectHighfieldsType2> list2 = subjectHighfieldsType2Repository.findByHighfields1(dto.getHighfields1(), szd);
            List<SubjectHighfieldsType2ResultLabel> list3 = new ArrayList<SubjectHighfieldsType2ResultLabel>();
            if (list2 != null && list2.size() > 0) {
                for (int j = 0; j < list2.size(); j++) {
                    SubjectHighfieldsType2 dto2 = list2.get(j);
                    SubjectHighfieldsType2ResultLabel subjectHighfieldsType2Result = new SubjectHighfieldsType2ResultLabel();
                    if (dto2 != null) {
                        if (dto2.getHighfields2() != null) {
                            subjectHighfieldsType2Result.setValue(dto2.getHighfields2());
                        }
                        if (dto2.getName() != null) {
                            subjectHighfieldsType2Result.setLabel(dto2.getName());
                        }
                        list3.add(subjectHighfieldsType2Result);
                    }
                }
                if (list3 != null && list3.size() > 0) {
                    for (int k = 0; k < list3.size(); k++) {
                        SubjectHighfieldsType2ResultLabel subjectHighfieldsType2Result = list3.get(k);
                        List<SubjectHighfieldsType3> list4 = subjectHighfieldsType3Repository.findByHighfields2(subjectHighfieldsType2Result.getValue(), szd);
                        if (list4 != null && list4.size() > 0) {
                            List<SubjectHighfieldsType3ResultLabel> list5 = new ArrayList<SubjectHighfieldsType3ResultLabel>();
                            for (int h = 0; h < list4.size(); h++) {
                                SubjectHighfieldsType3 dto3 = list4.get(h);
                                SubjectHighfieldsType3ResultLabel subjectHighfieldsType3Result = new SubjectHighfieldsType3ResultLabel();
                                if (dto3 != null) {
                                    if (dto3.getHighfields3() != null) {
                                        subjectHighfieldsType3Result.setValue(dto3.getHighfields3());
                                    }
                                    if (dto3.getName() != null) {
                                        subjectHighfieldsType3Result.setLabel(dto3.getName());
                                    }
                                    list5.add(subjectHighfieldsType3Result);
                                }
                            }
                            subjectHighfieldsType2Result.setChildren(list5);
                        }
                    }
                }
            }
            subjectHighfieldsType1Result.setLabel(dto.getName());
            subjectHighfieldsType1Result.setValue(dto.getHighfields1());
            subjectHighfieldsType1Result.setChildren(list3);
            newlist.add(subjectHighfieldsType1Result);
        }
        return newlist;
    }


    public List<SubjectHighfieldsType1QueryDto> queryAll(SubjectHighfieldsType1QueryCriteria criteria) {
        List<SubjectHighfieldsType1> subjectHighfieldsType1List = subjectHighfieldsType1Repository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root, criteria, criteriaBuilder));
        return subjectHighfieldsType1List.stream().map(subjectHighfieldsType1 -> subjectHighfieldsType1Mapper.toQueryDto(subjectHighfieldsType1)).collect(Collectors.toList());
    }


    @Transactional
    public SubjectHighfieldsType1QueryDto findById(Integer id) {
        SubjectHighfieldsType1 subjectHighfieldsType1 = subjectHighfieldsType1Repository.findById(id).orElseGet(SubjectHighfieldsType1::new);
        ValidationUtil.isNull(subjectHighfieldsType1.getId(), "SubjectHighfieldsType1", "id", id);
        return subjectHighfieldsType1Mapper.toQueryDto(subjectHighfieldsType1);
    }


    @Transactional(rollbackFor = Exception.class)
    public SubjectHighfieldsType1QueryDto create(SubjectHighfieldsType1CreateDto resources) {
        return subjectHighfieldsType1Mapper.toQueryDto(subjectHighfieldsType1Repository.save(subjectHighfieldsType1Mapper.toEntity(resources)));
    }


    @Transactional(rollbackFor = Exception.class)
    public void update(SubjectHighfieldsType1CreateDto resources) {
        SubjectHighfieldsType1 subjectHighfieldsType1 = subjectHighfieldsType1Repository.findById(resources.getId()).orElseGet(SubjectHighfieldsType1::new);
        ValidationUtil.isNull(subjectHighfieldsType1.getId(), "SubjectHighfieldsType1", "id", resources.getId());
        subjectHighfieldsType1.copy(subjectHighfieldsType1Mapper.toEntity(resources));
        subjectHighfieldsType1Repository.save(subjectHighfieldsType1);
    }


    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            subjectHighfieldsType1Repository.deleteById(id);
        }
    }


    public void download(List<SubjectHighfieldsType1QueryDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (SubjectHighfieldsType1QueryDto subjectHighfieldsType1 : all) {
            Map<String, Object> map = new LinkedHashMap<>();
            map.put(" highfields1", subjectHighfieldsType1.getHighfields1());
            map.put(" name", subjectHighfieldsType1.getName());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
}